using System;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Platform.Storage;
using Day.Data;
using MemorialDay.App.ViewModels;

namespace MemorialDay.App.Views;

public partial class MainView : UserControl
{
    private DayModel addModel = new();
    private DayModel AddModel
    {
        get => addModel;
        set
        {
            addModel = value;
            DayNameBox.Text = AddModel.DayName;
            DayIntroduceBox.Text = AddModel.DayIntroduce;
            try
            {
                DayTimeBox.SelectedDate = DateTimeOffset.Parse(AddModel.DayTime);
                DayImageButton.Background = new ImageBrush(new Bitmap(AddModel.DayImage));
            }
            catch
            {
                //
            }
        } 
    }
    private bool isChange;
    private bool IsChange
    {
        get => isChange;
        set
        {
            isChange = value;
            AddModelButton.Content = value ? "更改" : "添加";
        } 
    }

    public MainView()
    {
        InitializeComponent();
    }

    private void CardTapped(object? sender, TappedEventArgs e)
    {
        if (DataContext is not MainViewModel model) return;
        model.DetailedVisible = true;
    }

    private void AddDayClick(object? sender, RoutedEventArgs e)
    {
        AddModel = new DayModel();
        if (DataContext is not MainViewModel model) return;
        model.AddVisible = true;
        IsChange = false;
    }

    private async void AddImageClick(object? sender, RoutedEventArgs e)
    {
        var topLevel = TopLevel.GetTopLevel(this);
        if (topLevel == null) return;
        var resultList = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions());
        var result = resultList.FirstOrDefault();
        if (result == null) return;
        AddModel.DayImage = result.Path.LocalPath;
    }

    private async void AddModelClick(object? sender, RoutedEventArgs e)
    {
        AddModel.DayIntroduce = DayIntroduceBox.Text ?? "";
        AddModel.DayName = DayNameBox.Text!;
        AddModel.DayTime = DayTimeBox.SelectedDate!.Value.DateTimeOffsetToString();
        if (AddModel.IsNull()) return;
        if (DataContext is not MainViewModel model) return;
       
        if (IsChange)
        {
            model.AddVisible = !await model.Change(AddModel);
        }
        else
        {
            model.Add(AddModel);
            model.AddVisible = false;
        }

        IsChange = false;
    }

    private void CancelAddClick(object? sender, RoutedEventArgs e)
    {
        if (DataContext is not MainViewModel model) return;
        model.AddVisible = false;
        AddModel = new DayModel();
    }

    private void DayListChanged(object? sender, SelectionChangedEventArgs e)
    {
        if (sender is not ListBox box) return;
        if (box.SelectedItem is not DayModel day) return;
        if (DataContext is not MainViewModel model) return;
        model.ThisDay = day;
        model.DetailedVisible = true;
    }

    private void ChangeClick(object? sender, RoutedEventArgs e)
    {
        IsChange = true;
        if(sender is not Control control)return;
        if(control.DataContext is not DayModel day)return;
        AddModel = day;
        if (DataContext is not MainViewModel model) return;
        model.AddVisible = true;
    }

    private void BackClick(object? sender, RoutedEventArgs e)
    {
        if (DataContext is not MainViewModel model) return;
        model.AddVisible = false;
        model.DetailedVisible = false;
        model.InitVisible = true;
    }
}